layui.extend({
    excel: '{/}/static/lay/lib/extend/excel'
}).define(['excel'],function (exports) {
    let $ = layui.$;
    let excel = layui.excel;
    let customerExportExcel = {
        /**
         * 描述: 表格导出excel
         *
         * @param id 表格id
         * @param name 导出表格的名称
         * @author tanbiao
         * @since 20191101
         **/
        doExportExcel: function exportFile(id,name) {
            //根据传入tableID获取表头
            let headers = layui.$("#"+id).next().find("table").get(0);
            let htrs = Array.from(headers.querySelectorAll('tr'));
            let titles = {};
            for (let j = 0; j < htrs.length; j++) {
                let hths = Array.from(htrs[j].querySelectorAll("th"));
                let length =hths.length;
                //针对最后放大之后自适应会出现多出一列头部而单独处理
                let lastClazz =hths[length-1].getAttributeNode('class').value;
                if(lastClazz=='layui-table-patch'){
                    length --;
                }
                for (let i = 0; i < length-1; i++) {
                    let clazz = hths[i].getAttributeNode('class').value;
                    if (clazz != ' layui-table-col-special' && clazz != 'layui-hide' && clazz!='layui-table-patch') {
                        //排除居左、具有、隐藏字段
                        //修改:默认字段data-field+i,兼容部分数据表格中不存在data-field值的问题
                        titles['data-field' + i] = hths[i].innerText;
                    }
                }
            }
            //根据传入tableID获取table内容
            let bodys = $("div.layui-table-view .layui-table-box table").get(1);
            let btrs = Array.from(bodys.querySelectorAll("tr"))
            let bodysArr = new Array();
            for (let j = 0; j < btrs.length; j++) {
                let contents = {};
                let btds = Array.from(btrs[j].querySelectorAll("td"));
                for (let i = 0; i < btds.length-1; i++) {
                    for (let key in titles) {
                        //修改:默认字段data-field+i,兼容部分数据表格中不存在data-field值的问题
                        var field = 'data-field' + i;
                        if (field === key) {
                            //根据表头字段获取table内容字段
                            if (btds[i].innerHTML.indexOf('special-icon-export') != -1) {
                                var currentData = $(btds[i].innerHTML).find('img').attr('src');
                                if (currentData != null && currentData != undefined) {
                                    contents[field]=currentData;
                                } else {
                                    contents[field] = btds[i].innerText;
                                }
                            } else {
                                contents[field] = btds[i].innerText;
                            }
                        }
                    }
                }
                bodysArr.push(contents)
            }
            //将标题行置顶添加到数组
            bodysArr.unshift(titles);
            //导出excel
            excel.exportExcel({
                sheet1: bodysArr
            }, name + new Date().toLocaleString() + '.xlsx', 'xlsx');
        }
    }

    // 输出
    exports('zmnExportUtil', customerExportExcel);
});